package control;

import java.sql.SQLException;
import java.util.HashMap;

import model.Machine.MachineService;
import model.vo.WorkorderVO;

public class MachineController {
	private int production_quantity=0;
	private int production_bad_quantity1=0;
	private int production_bad_quantity11=0;
	private int production_bad_quantity2=0;
	private int production_bad_quantity22=0;
	private int production_bad_quantity3=0;
	private int production_bad_quantity4=0;
	private String production_state = "stop";
	private MachineService machineService;
	public void setmachineService(MachineService machineService){
		this.machineService=machineService;
	}
	// 생산할때마다 hitory 디비에 넣는거 ..
	// 처음에는 insert 그후에는 update
	// 오류시에는 insert
	// 그 후 성공시 다시 insert
	public void workOrderInsert(int workorder_quantity,int speed){
		production_state = "start";
		try {
			Thread t = new Thread();
			for(int i=1; i<=workorder_quantity; i++){
				if((i%5)==1){
					machineService.soapbaseUse();
				}
				t.sleep(speed);
				int random = (int)(((double)Math.random())*100);
				if(speed==1500){
					if(random==1 || random==3){  
		//				1 공정에서 실패했을떄 들어가는 insert문 i번쨰 씨리얼
						machineService.oilUse();
						if(random==1){
							machineService.production_bad_quantity(1,i);
							production_bad_quantity1+=1;
							++workorder_quantity; //진짜 불량
						}else{
							machineService.production_bad_quantity(3,i);
							production_bad_quantity11+=1;  // 재가공불량
							production_quantity+=1;
							machineService.production_quantity();
						}
					}else if(random==2 || random==4){
		//				2 공정에서 실패했을떄 들어가는 insert문 i번쨰 씨리얼
						machineService.hardenerUse();
						if(random==2){
							machineService.production_bad_quantity(2,i);
							production_bad_quantity2+=1;
							++workorder_quantity;
						}else{
							machineService.production_bad_quantity(4,i);
							production_bad_quantity22+=1;
							production_quantity+=1;
							machineService.production_quantity();
						}				
					}else if(random==5 || random==6){
						production_bad_quantity3+=1;
		//				3 공정에서 실패했을떄 들어가는 insert문 i번쨰 씨리얼
						machineService.production_bad_quantity(5,i);
						production_quantity+=1;
						machineService.production_quantity();
					}else if(random==7 || random==8){
						production_bad_quantity4+=1;
		//				4 공정에서 실패했을떄 들어가는 insert문 i번쨰 씨리얼
						machineService.production_bad_quantity(6,i);
						machineService.wrapperUse();
						production_quantity+=1;
						machineService.production_quantity();
					}else{
						production_quantity+=1;
						//모든 생산 insert 2번쨰 넣는거는 service에서 비교후 update하기 
						machineService.production_quantity();
						machineService.oilUse();
						machineService.wrapperUse();
						machineService.hardenerUse();
					}
				}else if(speed==700){
					if(random==1 || random==3 || random==7){  
		//				1 공정에서 실패했을떄 들어가는 insert문 i번쨰 씨리얼
						machineService.oilUse();
						if(random==3){
							machineService.production_bad_quantity(3,i);
							production_bad_quantity11+=1;  // 재가공불량
							production_quantity+=1;
							machineService.production_quantity();
						}else{
							machineService.production_bad_quantity(1,i);
							production_bad_quantity1+=1;
							++workorder_quantity; //진짜 불량
						}
					}else if(random==2 || random==4 || random==8){
		//				2 공정에서 실패했을떄 들어가는 insert문 i번쨰 씨리얼
						machineService.hardenerUse();
						if(random==4){
							machineService.production_bad_quantity(4,i);
							production_bad_quantity22+=1;
							production_quantity+=1;
							machineService.production_quantity();
						}else{
							machineService.production_bad_quantity(2,i);
							production_bad_quantity2+=1;
							++workorder_quantity;
							}				
						}else if(random==5 || random==6 || random==9){
							production_bad_quantity3+=1;
						//				3 공정에서 실패했을떄 들어가는 insert문 i번쨰 씨리얼
							machineService.production_bad_quantity(5,i);
							production_quantity+=1;
							machineService.production_quantity();
						    }else if(random==7 || random==8 || random==10){
							production_bad_quantity4+=1;
						//				4 공정에서 실패했을떄 들어가는 insert문 i번쨰 씨리얼
							machineService.production_bad_quantity(6,i);
							machineService.wrapperUse();
							production_quantity+=1;
							machineService.production_quantity();
						}else{
						production_quantity+=1;
						//모든 생산 insert 2번쨰 넣는거는 service에서 비교후 update하기 
						machineService.production_quantity();
						machineService.oilUse();
						machineService.wrapperUse();
						machineService.hardenerUse();
						}
				}else{
					if(random==1 || random==3 || random==7 || random==8){  
		//				1 공정에서 실패했을떄 들어가는 insert문 i번쨰 씨리얼
						machineService.oilUse();
						if(random==3){
							machineService.production_bad_quantity(3,i);
							production_bad_quantity11+=1;  // 재가공불량
							production_quantity+=1;
							machineService.production_quantity();
						}else{
							machineService.production_bad_quantity(1,i);
							production_bad_quantity1+=1;
							++workorder_quantity; //진짜 불량
						}
					}else if(random==2 || random==4 || random==9 || random==10){
		//				2 공정에서 실패했을떄 들어가는 insert문 i번쨰 씨리얼
						machineService.hardenerUse();
						if(random==4){
							machineService.production_bad_quantity(4,i);
							production_bad_quantity22+=1;
							production_quantity+=1;
							machineService.production_quantity();
						}else{
							machineService.production_bad_quantity(2,i);
							production_bad_quantity2+=1;
							++workorder_quantity;
							}				
						}else if(random==5 || random==6 || random==11 || random==12){
							production_bad_quantity3+=1;
						//				3 공정에서 실패했을떄 들어가는 insert문 i번쨰 씨리얼
							machineService.production_bad_quantity(5,i);
							production_quantity+=1;
							machineService.production_quantity();
						    }else if(random==7 || random==8 || random==13 || random==14){
							production_bad_quantity4+=1;
						//				4 공정에서 실패했을떄 들어가는 insert문 i번쨰 씨리얼
							machineService.production_bad_quantity(6,i);
							machineService.wrapperUse();
							production_quantity+=1;
							machineService.production_quantity();
						}else{
						production_quantity+=1;
						//모든 생산 insert 2번쨰 넣는거는 service에서 비교후 update하기 
						machineService.production_quantity();
						machineService.oilUse();
						machineService.wrapperUse();
						machineService.hardenerUse();
						}
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		production_state = "stop";
	}
	public HashMap production_view(){
		HashMap production_map = new HashMap();
		production_map.put("production_quantity", production_quantity);
		production_map.put("production_bad_quantity1", production_bad_quantity1);
		production_map.put("production_bad_quantity2", production_bad_quantity2);
		production_map.put("production_bad_quantity11", production_bad_quantity11);
		production_map.put("production_bad_quantity22", production_bad_quantity22);
		production_map.put("production_bad_quantity3", production_bad_quantity3);
		production_map.put("production_bad_quantity4", production_bad_quantity4);
		production_map.put("production_state", production_state);
		try {
			Thread t = new Thread();
			t.sleep(1500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return production_map;	
	}
	public String production_stateView(){
		return 	production_state;	
	}
	// 운영자 지시사항을 확인시,
	public WorkorderVO workorderCheck() throws SQLException{
		WorkorderVO wvo = machineService.workorderCheck();
		return wvo;
	}
	public void setProduction_quantity(int production_quantity){
		this.production_quantity=production_quantity;
	}
	//초기화
	public void setProduction_bad_quantity(int production_bad_quantity1,int production_bad_quantity11,int production_bad_quantity22,int production_bad_quantity2,int production_bad_quantity3,int production_bad_quantity4){
		this.production_bad_quantity1=production_bad_quantity1;
		this.production_bad_quantity11=production_bad_quantity11;
		this.production_bad_quantity2=production_bad_quantity2;
		this.production_bad_quantity22=production_bad_quantity22;
		this.production_bad_quantity3=production_bad_quantity3;
		this.production_bad_quantity4=production_bad_quantity4;		
	}
}
